package com.ruoyi.classroom.question.service.impl;

import com.ruoyi.classroom.question.domain.Question;
import com.ruoyi.classroom.question.domain.QuestionCourse;
import com.ruoyi.classroom.question.mapper.QuestionMapper;
import com.ruoyi.classroom.question.service.IQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 答题管理Service业务层处理
 * 
 * @author lgm
 * @date 2024-07-26
 */
@Service
public class QuestionServiceImpl implements IQuestionService
{
    @Autowired
    private QuestionMapper questionMapper;

    /**
     * 查询答题管理
     * 
     * @param id 答题管理主键
     * @return 答题管理
     */
    @Override
    public Question selectQuestionById(Long id)
    {
        return questionMapper.selectQuestionById(id);
    }

    /**
     * 查询答题管理列表
     * 
     * @param question 答题管理
     * @return 答题管理
     */
    @Override
    public List<Question> selectQuestionList(Question question)
    {
        return questionMapper.selectQuestionList(question);
    }

    @Override
    public List<Question> selectQuestionListAnser(Question question) {
        return questionMapper.selectQuestionListAnswer(question);
    }

    /**
     * 新增答题管理
     * 
     * @param question 答题管理
     * @return 结果
     */
    @Override
    public int insertQuestion(Question question)
    {
        question.setQuestiontime(new Date());
        int result = questionMapper.insertQuestion(question);
//        遍历获取到刚刚插入到的最后一条数据
        Question insertQuestion = questionMapper.selectQuestionById(question.getId());
        if (insertQuestion != null){
            Long questionId = insertQuestion.getId();
            Long courseId = question.getCourseId();
            QuestionCourse questionCourse = new QuestionCourse();
            questionCourse.setQuestionId(questionId);
            questionCourse.setCourseId(courseId);
            questionMapper.insertQuestionCourse(questionCourse);
            return 1;
        }else {
            return 0;
        }
    }

    /**
     * 修改答题管理
     * 
     * @param question 答题管理
     * @return 结果
     */
    @Override
    public int updateQuestion(Question question)
    {
//        设置提问时间
        question.setQuestiontime(new Date());
        QuestionCourse questionCourse = questionMapper.selectQuestionCourseList(question.getId());
//        为关联表插入数据
        questionCourse.setQuestionId(question.getId());
        questionCourse.setCourseId(question.getCourseId());
        questionCourse.setUserId(question.getUserId());
//      如果studentanswer不为空，就设置答题时间answertime
        if (question.getStudentanswer()!=null){
            question.setAnswertime(new Date());
        }
        int result = questionMapper.updateQuestionCourse(questionCourse);
        int updateQuestion = questionMapper.updateQuestion(question);
        if (result>0 && updateQuestion>0){
            return 1;
        }else {
            return 0;
        }
    }

    /**
     * 批量删除答题管理
     * 
     * @param ids 需要删除的答题管理主键
     * @return 结果
     */
    @Override
    public int deleteQuestionByIds(Long[] ids)
    {
        questionMapper.deleteQuestionCourseById(ids);
        return questionMapper.deleteQuestionByIds(ids);
    }

    /**
     * 删除答题管理信息
     * 
     * @param id 答题管理主键
     * @return 结果
     */
    @Override
    public int deleteQuestionById(Long id)
    {
        return questionMapper.deleteQuestionById(id);
    }
}
